import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let liststr = localStorage.getItem('primary')
let list = liststr ? JSON.parse(liststr) : []
export default new Vuex.Store({
  state: {
    list: list//{ id,title,content,time}
  },
  mutations: {
    addNote(state, option) {
      //state就是上边的那个state
      //option是调用这个方法的人传递的参数
      // state.list = [option].concat(state.list)
      state.list = [option, ...state.list]
      localStorage.setItem('primary', JSON.stringify(state.list))
    },
    removeNote(state, option) {
      state.list = state.list.filter(item => item.id !== option)
      localStorage.setItem('primary', JSON.stringify(state.list))
    },
    updateNote(state, option) {
      let index = state.list.findIndex(item => item.id == option.id)
      state.list.splice(index, 1, option)
      localStorage.setItem('primary', JSON.stringify(state.list))
    }
  }
})
